package com.teacold.customdemo.chapter_five;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;

public class GetSegmentView extends View {
    private Paint mPaint;
    private Path mPath,mCirclePath;
    private PathMeasure mPathMeasure;
    private Float mCuranimValue;

    public GetSegmentView(Context context) {
        super(context);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.BLUE);
        mPath=new Path();
        mCirclePath=new Path();
        mCirclePath.addCircle(100,100,50,Path.Direction.CW);
        mPathMeasure=new PathMeasure(mCirclePath,true);
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,1);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCuranimValue= (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setDuration(3000);
        valueAnimator.start();
    }

    public GetSegmentView(Context context, AttributeSet attrs) {
        this(context);
    }

    public GetSegmentView(Context context,AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        float stop=mPathMeasure.getLength()*mCuranimValue;
        mPath.reset();
        mPathMeasure.getSegment(0,stop,mPath,true);
        canvas.drawPath(mPath,mPaint);
        
    }
}
